#!/usr/bin/awk -f

function trim(str)
{
	sub(/^[ \t]*/,"",str);
	sub(/[ \t]*$/,"",str);
	return str
}

function symbol(str)
{
	sub(/[ \t]*=.*/,"",str);
	return str
}
BEGIN {
	FS=":"
	RS="\n"
	step = "init"
	headerfile = ""
	cppfile = ""
#	print "*****"
}

{
	content = trim($0)
	if( content != "" ){
		if( "$$" == content ){
			if( "init" == step ){
				step = "head"
			}else if( "head" == step ){
				step = "cpp-head"
			}else if( "cpp-head" == step ){
				step = "body"
			}else if( "body" == step ){
				step = "tail"
			}else if( "tail" == step ){
				step = "cpp-tail"
			}else if( "cpp-tail" == step ){
				exit
			}
		}else{
			if( "init" == step ){
				if ( "" == headerfile ){
					headerfile = content;
				}else{
					cppfile = content;
				}
			}else if( "head" == step ){
				print $0 > headerfile
			}else if( "cpp-head" == step ){
				print $0 > cppfile
			}else if( "body" == step ){
				errno = trim($1)
				description = trim($2)
				if( "" != headerfile ){
					if( "" != errno){
						print "\t"errno"," >headerfile
					}
				}
				if( "" != cppfile ){
					if( "" != description ){
						print "\t{ "symbol(errno)" , \""symbol(errno)": "description"\" }," >cppfile
					}
				}
			}else if( "tail" == step ){
				print $0 >headerfile
			}else if( "cpp-tail" == step ){
				print $0 >cppfile
			}
		}
	}
#	print step
}
END{
#	print "###"
}
